<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, ContractList, ContractOrder, ContractReser, ContractSell, Config};
use think\Db;
use app\facade\Redis;

class MatchFreed extends Command
{
    protected function configure()
    {
        $this->setName('MatchFreed')->setDescription('合约释放收益');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/rskj/think MatchFreed'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        $contracts = (new Config())->where('key', 'contracts')->value('value');
        $contracts = json_decode($contracts, true);
        $contracts['gener'] = !empty($contracts['gener']) ? $contracts['gener'] : 0;
        $contracts['seconf'] = !empty($contracts['seconf']) ? $contracts['seconf'] : 0;
        $list = (new ContractOrder())->limit(100)->where('incometime', '<', date('Y-m-d'))->where(['income_status' => 2, 'status' => 3])->select();
        foreach ($list as $lists) {
            $money = round($lists['product_price'] * $lists['figure'] / ($lists['day'] * 100), 2);
            $one =  round($money * $contracts['gener'] / 100, 2);
            $two =  round($money * $contracts['seconf'] / 100, 2);
            $user = (new Users())->where('id', $lists['user_id'])->find();
            if ($user['pid'] > 0 && $one > 0) {
                $puser = (new Users())->where('id', $user['pid'])->find();
            }
            if ($user['ppid'] > 0 && $two > 0) {
                $ppuser = (new Users())->where('id', $user['ppid'])->find();
            }
            $price = $lists['price'] + $money;
            $income_status = 2;
            if ($lists['idays'] <= 1) {
                $income_status = 3;
            }
            if ($income_status == 3) {
                if ($price >= $lists['maxprice']) {
                    $sell = [[
                        'user_id' => $lists['user_id'],
                        'product_id' => $lists['product_id'],
                        'product_name' => $lists['product_name'],
                        'orderid' => $lists['id'],
                        'buyprice' => $lists['product_price'],
                        'price' => round($price / 2, 2),
                        'status' => 1
                    ], [
                        'user_id' => $lists['user_id'],
                        'product_id' => $lists['product_id'],
                        'product_name' => $lists['product_name'],
                        'orderid' => $lists['id'],
                        'buyprice' => $lists['product_price'],
                        'price' => round($price / 2, 2),
                        'status' => 1
                    ]];
                } else {
                    $sell = [
                        'user_id' => $lists['user_id'],
                        'product_id' => $lists['product_id'],
                        'product_name' => $lists['product_name'],
                        'orderid' => $lists['id'],
                        'buyprice' => $lists['product_price'],
                        'price' => $price,
                        'status' => 1
                    ];
                }
            }
            Db::startTrans();
            try {
                (new ContractOrder())->where('id', $lists['id'])->update([
                    'idays' => ($lists['idays'] - 1),
                    'price' => $price,
                    'incometime' => date('Y-m-d H:i:s'),
                    'income_status' => $income_status,
                    'todayincome' => $money,
                    'allincome' => ($lists['allincome'] + $money)
                ]);
                //一代、二代奖励（待添加）
                if (!empty($puser) > 0 && $one > 0) {
                    (new Users())->MoneyEdit($puser['id'], $puser['assets'], $one, 4, 2, '一代收益', $lists['id'], $lists['user_id'], 0, 0, $one);
                }
                if (!empty($ppuser) > 0 && $two > 0) {
                    (new Users())->MoneyEdit($ppuser['id'], $ppuser['assets'], $two, 4, 3, '二代收益', $lists['id'], $lists['user_id'], 0, 0, $two);
                }
                //判断是否完结
                if (!empty($sell)) {
                    (new ContractSell())->insertAll($sell);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                dump($e->getMessage());
            }
            echo '释放成功';
        }
    }
}
